package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.train.dao.RouteScheduleRelationDao;
import com.my12306.train.model.entity.RouteScheduleRelationEntity;
import com.my12306.train.model.vo.RouteScheduleVo;
import com.my12306.train.service.RouteScheduleRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("routeScheduleRelationService")
public class RouteScheduleRelationServiceImpl extends ServiceImpl<RouteScheduleRelationDao, RouteScheduleRelationEntity> implements RouteScheduleRelationService {


    @Override
    public PageUtils queryRouteScheduleByPage(PageVo pageVo) {
        pageVo = pageVo.init();
        List<RouteScheduleVo> dataList = this.baseMapper.queryRouteScheduleByPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        int dataCount = this.queryRouteScheduleCountByPage(pageVo,CommonConstants.LogicDelete.no_delet.getValue());
        PageUtils pageUtils = PageUtils.build(pageVo, dataList, dataCount);
        return pageUtils;
    }

    @Override
    public Integer queryRouteScheduleCountByPage(PageVo pageVo, Integer logicDelete) {
       return this.baseMapper.queryRouteScheduleCountByPage(pageVo,logicDelete);
    }

    @Override
    public Long saveRouteSchedule(RouteScheduleRelationEntity routeSchedule) {
        if (routeSchedule!=null){
            routeSchedule.setCreateDate(new Date());
            routeSchedule.setUpdateDate(new Date());
            routeSchedule.setId(SnowflakeIdWorker.generateId());
            int insert = this.baseMapper.insert(routeSchedule);
            System.out.println("添加数据的结果："+insert);
            if (insert>0){
                return routeSchedule.getId();
            }else{
                return null;
            }
        }
        return null;
    }

    @Override
    public Long updateRouteScheduleById(RouteScheduleRelationEntity routeSchedule) {
        int flag = this.baseMapper.updateById(routeSchedule);
        if (flag>0){
            return routeSchedule.getId();
        }else{
            return null;
        }
    }

    @Override
    @Transactional
    public void deleteRouteScheduleByIds(List<Long> ids,int logicDelete) {
        if (ids!=null&&ids.size()>0){
            this.baseMapper.deleteRouteScheduleBatchByIds(ids,logicDelete);
        }
    }

    @Override
    @Transactional
    public void deleteRouteScheduleByRouteId(List<Long> routeIds,int logicDelete) {
        if (routeIds!=null&&routeIds.size()>0){
            this.baseMapper.deleteRouteScheduleBatchByRouteIds(routeIds,logicDelete);
        }
    }
}
